<?php

namespace App\Services\Admin\Message;

use App\Exceptions\MessageException;
use Exception;

class MessageService extends BaseMessageService
{

    /**
     * 公告消息列表
     * @return array
     * @throws Exception
     */
    public function list(): array
    {
        $total = $this->getSysMessageModel()->total();
        $messageList = $this->getSysMessageModel()->list([
            'id', 'title', 'describe', 'status'
        ]);

        $list = [];
        foreach ($messageList as $message) {
            $list[] = [
                'id' => $message['id'],
                'title' => $message['title'],
                'describe' => $message['describe'],
                'status' => $message['status']
            ];
        }

        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 公告消息详情
     * @return void
     * @throws Exception
     */
    public function detail(array $params): array
    {
        $detail = $this->getSysMessageModel()->getItemById($params['id'], [
            'id', 'title', 'describe', 'status'
        ]);
        if (empty($detail)) {
            throw new MessageException('公告不存在', MessageException::CODE_1001);
        }

        return $detail;
    }

    /**
     * 创建公告消息
     * @param array $params
     * @return void
     * @throws Exception
     */
    public function create(array $params)
    {
        $createData = [
            'title' => $params['title'],
            'describe' => $params['describe'] ?? '',
        ];

        $this->getSysMessageModel()->addItem($createData);
    }

    /**
     * 更新公告消息
     * @return void
     * @throws Exception
     */
    public function update(array $params)
    {
        $detail = $this->getSysMessageModel()->getItemById($params['id'], [
            'id', 'title', 'describe', 'status'
        ]);
        if (empty($detail)) {
            throw new MessageException('公告不存在', MessageException::CODE_1001);
        }

        $updateData = [];
        if (!empty($params['title'])) {
            $updateData['title'] = $params['title'];
        }
        if (!empty($params['describe'])) {
            $updateData['describe'] = $params['describe'];
        }

        $this->getSysMessageModel()->updateItemById($params['id'], $updateData);
    }

    /**
     * 删除公告消息
     * @param array $params
     * @return void
     * @throws MessageException
     * @throws Exception
     */
    public function delete(array $params)
    {
        $detail = $this->getSysMessageModel()->getItemById($params['id'], [
            'id', 'title', 'describe', 'status'
        ]);
        if (empty($detail)) {
            throw new MessageException('公告不存在', MessageException::CODE_1001);
        }

        $this->getSysMessageModel()->deleteItemById($params['id']);
    }

    /**
     * 上架/下架
     * @return void
     * @throws Exception
     */
    public function release(array $params)
    {
        $detail = $this->getSysMessageModel()->getItemById($params['id'], [
            'id', 'title', 'describe', 'status'
        ]);
        if (empty($detail)) {
            throw new MessageException('公告不存在', MessageException::CODE_1001);
        }

        $updateData = [
            'status' => $params['status'],
        ];

        $this->getSysMessageModel()->updateItemById($params['id'], $updateData);
    }
}
